<?php
include_once('include/top.php');

$action=fs_get_value("do");
switch($action)
{
	case    "add"       :   add()       ;   break;
	case    "do_add"    :   do_add()    ;   break;
	case    "do_edit"   :   do_edit()   ;   break;
	case    "delete"    :   do_delete()    ;   break;
    case    "editpassword": editpassword();break;
    case    "updateinfo":     updateinfo();break;
	default				:	main()		;	break;
}

?>
<script src="./js/Main.js"></script>
<script type="text/javascript">
 function getOptionMAJOR(){
  $.getJSON("option.php",{'bval':$("#departid").val()},function(data){
    $("#majorid").empty();//每次选择前，清空小类的值。
    $("#majorid").html("<option value='-1'>请选择专业</option>");
    $.each(data, function(index,array){
     var option="<option value='"+array['id']+"' >"+array['name']+"</option>"; 
     $("#majorid").append(option);
    });                
   }); 
 }
 $(document).ready(function(){
    $("#departid").change(function(){
        getOptionMAJOR();
    });
 });
</script>
<script type="text/javascript">
window.onload = function()
{
        CKEDITOR.replace( 'introduce',{ height : '150px', width:'600px',toolbar:'MyToolbar',fontSize_sizes:'16/16px' } );
};
</script>
<?php
function main(){
    global $db,$teacher_id,$teacher_passport;
	$sql = "select * from nettteach_member where id = '".$teacher_id."'";
	$a_result = $db -> query($sql);
	$a_row = $db -> fetch_array($a_result);
?>	
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="CContent">
		<tr>
		    <th class="CTitle">个人资料</th>
		</tr>
        <tr>
            <td class="CPanel">
            <div class="SubMenu">
                <a href="?" class="lj">个人资料</a> | 
                <a href="?do=editpassword" class="lj">修改密码</a> | 
                <a href="?do=delete" class="" onclick="return confirm('你确定注销自己吗？注销后将不能再使用此账号登陆。')">注销自己</a>
            </div>
			<form name="addform" method="post" action="?do=updateinfo">
			<fieldset>
				<legend>编缉教师</legend>
                带<span style="color:#FF0000;">*</span>的必填。<br/><br/>
                    <label>用户名：</label><?php echo($a_row['passport']);?><br/><br/>
                    <label>教师姓名：</label><input name="member_name" type="text"  class="text" value="<?php echo($a_row['realname']);?>" /><span style="color:#FF0000;">*</span><br/><br/>
                    <label>院系：</label>
                        <select name="departid" id="departid" style="width:300px;" >
                                        <option value="-1">请选择系</option>
                                        <?php
                                        $sql="select* from nettteach_department where 1=1";
                                        $result=mysql_query($sql);
                                        //循环列出选项
                                                while($rows=mysql_fetch_array($result))
                                        {?>
                                                <option value="<?php echo $rows['id']; ?>" <?php if($rows['id']==$a_row['departmentid'])echo 'selected'; ?>><?php echo $rows['name'];?></option>
                                        <?php } ?>
                        </select><span style="color:#FF0000;">*</span><br/><br/>
                        <label>专业：</label>
                        <select name="majorid" id="majorid" style="width:300px;">
                             <option value="-1">请选择专业</option>
                                        <?php
                                        $sql="select* from nettteach_major where departmentid=".$a_row['departmentid'];
                                        $result=mysql_query($sql);
                                        //循环列出选项
                                                while($rows=mysql_fetch_array($result))
                                        {?>
                                        <option value="<?php echo $rows['id']; ?>" <?php if($rows['id']==$a_row['majorid'])echo 'selected'; ?>><?php echo $rows['name'];?></option>
                                        <?php } ?>
                        </select><span style="color:#FF0000;">*</span><br/><br/>
                    
                    <label>名师类型：</label>
					<select name="star" style="width:300px;" disabled>
<!--					    <option value="" <?php if($a_row['star'] == null){echo("selected");}?>>未知</option>-->
					    <option value="0" <?php if($a_row['star'] == 0){echo("selected");}?>>普通教师</option>
					    <option value="1" <?php if($a_row['star'] == 1){echo("selected");}?>>国家级名师</option>
                        <option value="2" <?php if($a_row['star'] == 2){echo("selected");}?>>省级名师</option>
                        <option value="3" <?php if($a_row['star'] == 3){echo("selected");}?>>校级名师</option>
					</select><span style="color:#FF0000;">*</span><br/><br/>
                    <label>教师状态：</label>
                    <select name="state" style="width:300px;">
					    <option value="0" <?php if($a_row['state'] == 0){echo("selected");}?>>未激活</option>
					    <option value="1" <?php if($a_row['state'] == 1){echo("selected");}?>>已激活</option>
					</select><span style="color:#FF0000;">*</span><br/><br/>
                    <label>联系电话：</label><input name="tel" type="text"  class="text" value="<?php echo($a_row['tel']);?>"><br/><br/>
					<label>电子邮件：</label><input name="email" type="text"  class="text" value="<?php echo($a_row['email']);?>"><br/><br/>
                    <label>qq：</label><input name="qq" type="text"  class="text" value="<?php echo($a_row['qq']);?>"><br/><br/>
                    <label>登陆次数：</label><input name="logintimes" type="text"  class="text" value="<?php echo($a_row['logintimes']);?>"><br/><br/>
					<input type="hidden" name="lastphoto" value="<?php echo($a_row['photo']);?>" style="width:300px;" />
                    <label>头像上传：</label><input type="hidden" name="photo" value="<?php echo($a_row['photo']);?>" style="width:300px;" /><br /><br />
					<label></label><iframe frameborder="0" name="upload_frame" scrolling="no" src="../backstage/admin_upload.php" width="280" onload="this.height=this.contentWindow.document.body.scrollHeight"></iframe><br />
                    <label></label><span>只能上传小于10M的图片，格式仅限于jpg,gif,png,bmp,jpeg。</span><br/><br/>
                    <?php if($a_row['star'] == 1){?>
                    <label>名师简介：</label><div style="padding-left:100px"><textarea name="introduce"><?php echo($a_row['introduce']);?></textarea></div><br/><br/>
                    <?php }?>
                    <div class="submit">
					<label></label><input type="hidden" name="test_major" value="1" /><input type="submit" class="sub" value="确认提交" onclick="return Check();"/>&nbsp;&nbsp;&nbsp;&nbsp;<a class="lj" href="admin_teacher.php">返回列表</a></div>
                                 
                                        </fieldset>
			</form></div>
			</td>
		</tr>
	</table>
<?php
}?>
<?php
function editpassword(){
    global $db,$teacher_id,$teacher_passport;
	$sql = "select * from nettteach_member where id = '".$teacher_id."'";
	$a_result = $db -> query($sql);
	$a_row = $db -> fetch_array($a_result);
?>	
<script>
function check()
{
	if (document.editform.old_password.value == '')
	{
		alert("请输入原密码！");
		return false;
	}
	if (document.editform.new_password.value == '')
	{
		alert("请输入新密码！");
		return false;
	}
	if (document.editform.true_password.value == '')
	{
		alert("请输入确认密码！");
		return false;
	}
	if (document.editform.new_password.value != document.editform.true_password.value)
	{
		alert("两次密码不一致！");
		return false;
	}
}
</script>
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="CContent">
		<tr>
		    <th class="CTitle">个人资料</th>
		</tr>
        <tr>
            <td class="CPanel">
            <div class="SubMenu">
                <a href="?" class="lj">个人资料</a> | 
                <a href="?do=editpassword" class="lj">修改密码</a> | 
                <a href="?do=delete" class="" onclick="return confirm('你确定注销自己吗？注销后将不能再使用此账号登陆。')">注销自己</a>
            </div>
			<form name="editform" method="post" action="?do=do_edit">
			<fieldset>
				<legend>修改密码</legend>
				<div style="text-align:left;">
					<table border="0" cellpadding="2" cellspacing="1">
						<tr>
							<td height="30" width="100"><div align="right">用户名：</div></td>
							<td><?php echo($teacher_passport);?></td>
						</tr>
						<tr>
							<td height="30"><div align="right">原密码：</div></td>
							<td><input type="password" name="old_password" value="" style="width:180px;" /></td>
						</tr>
						<tr>
							<td height="30"><div align="right">新密码：</div></td>
							<td><input type="password" name="new_password" value="" style="width:180px;" /></td>
						</tr>
						<tr>
							<td height="30"><div align="right">确认新密码：</div></td>
							<td><input type="password" name="true_password" value="" style="width:180px;" /></td>
						</tr>
						<tr>
							<td height="30"></td>
							<td><div class="submit" align="center" style="width:60%; height:30px; line-height:30px;">
							<input type="submit" name="submit" value="确定修改" onclick="return check()"/></div></td>
						</tr>
					</table>
				</div>
			</fieldset>
			</form>
			</td>
		</tr>
	</table>
<?php
}
function do_edit(){
    global $db,$teacher_id;
	$old_password = md5(fs_get_value("old_password"));
        
	$password = fs_get_value("new_password");
	$true_password = fs_get_value("true_password");
	$sql = "select * from nettteach_member where id = ".$teacher_id;
	$main_result = $db -> query($sql);
	$main_row = $db -> fetch_array($main_result);
	if($old_password == $main_row['password']){
		if($password == $true_password){
			$sql = "update nettteach_member set password = '". md5($password)."' where id = ".$teacher_id;
			$db -> query($sql);
			fs_showmsg("修改成功！");
		}
		else{
			fs_showmsg("两次密码输入不一致！");
		}
	}
	else{
		fs_showmsg("旧密码输入不正确！");
	}
}
?>

<?php
function do_delete(){
    global $db,$teacher_id;
	$sql = "delete from nettteach_member where id = '".$teacher_id."'";
	$db -> query($sql);
	session_unset($_SESSION['teacher']);
	fs_showmsg("您已注销自己的账号！","index.php");
}
?>
<?php
function updateinfo(){
    global $db,$teacher_id;
	$member_name = fs_get_value("member_name");
	$password = fs_get_value("password");
	$tel = fs_get_value("tel");
	$email = fs_get_value("email");
    $qq = fs_get_value("qq");
	$majorid = fs_get_value("majorid");
	$departid = fs_get_value("departid");
	$state = fs_get_value("state");
	$logintimes = fs_get_value("logintimes");
	$star = fs_get_value("star");
    $photo = fs_get_value("photo");
    $introduce = fs_get_value("introduce");
     $lastphoto = fs_get_value("lastphoto"); //原头像地址 若换了则删除原文件
                if($teacher_id == ''){
                    fs_showmsg("请登录");
                }
                else{
                        //修改教师
                        if($member_name != ''){
                                $sql = "update nettteach_member  set realname = '".$member_name."' where id = ".$teacher_id;
                                $db -> query($sql);
                        }
                
                        if($password != ''){
                                $sql = "update nettteach_member  set password = '".fs_sha1($password)."' where id = ".$teacher_id;
                                $db -> query($sql);
                        }
                        if($photo != '' && $photo !=$lastphoto){
                            $sql = "update nettteach_member set photo = '".$photo."' where id = ".$teacher_id;
                            $db -> query($sql);
                            $file="../backstage/".$lastphoto; 
                            if (unlink($file)) {
//                               echo "The file was deleted successfully.", "n";
                            } else {
//                               echo "The specified file could not be deleted. Please try again.", "n";
                            }
                        }
                        if($introduce != ''){
                                $sql = "update nettteach_member set introduce = '".$introduce."' where id = ".$teacher_id;
                                $db -> query($sql);
                        }
//                        if($tel != ''){
                                $sql = "update nettteach_member set tel = '".$tel."' where id = ".$teacher_id;
                                $db -> query($sql);
//                        }
//                        if($email != ''){
                                $sql = "update nettteach_member set email = '".$email."' where id = ".$teacher_id;
                                $db -> query($sql);
                                $sql = "update nettteach_member set qq = '".$qq."' where id = ".$teacher_id;
                                $db -> query($sql);
//                        }
                        if($majorid != '-1' && $majorid != ''){
                                $sql = "update nettteach_member set majorid = ".$majorid." where id = ".$teacher_id;
                                $db -> query($sql);
                        }
                        if($departid != '-1'){
                                $sql = "update nettteach_member set departmentid = ".$departid." where id = ".$teacher_id;
                                $db -> query($sql);
                        }
                        if($state != ''){
                                $sql = "update nettteach_member set state = ".$state." where id = ".$teacher_id;
                                $db -> query($sql);
                        }
                        else{
                                $sql = "update nettteach_member set state = null where id = ".$teacher_id;
                                $db -> query($sql);
                        }
                        if($logintimes != ''){
                                $sql = "update nettteach_member set logintimes = ".$logintimes." where id = ".$teacher_id;
                                $db -> query($sql);
                        }
                        fs_showmsg("修改成功！");
                }
}
?>